package model.preprocessor;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import model.NoSuchDirectoryException;
import controller.preprocessor.Hypre;
import controller.preprocessor.PreProcessor;
import controller.preprocessor.ReViVal;
import controller.preprocessor.SatEllite1_0;

/**
 * Manage the creation of preprocessor.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class PreProcessorFactory {

	private File				directory;
	private Set<EPreProcessor>	available	= new HashSet<EPreProcessor>();

	/**
	 * Create a preprocessor factory.
	 * 
	 * @param directory
	 *            the directory where are the executable file.
	 */
	public PreProcessorFactory(File directory) {
		this.directory = directory;
		for (EPreProcessor p : EPreProcessor.values())
			available.add(p);
		// JOptionPane.showMessageDialog(null, directory, "", JOptionPane.WARNING_MESSAGE);
		// JOptionPane.showMessageDialog(null, new File(".").getAbsolutePath(), "",
		// JOptionPane.WARNING_MESSAGE);
		// System.out.println(new File(".").getAbsolutePath());

	}

	/**
	 * @return the available preprocessor.
	 */
	public Set<EPreProcessor> getAvailable() {
		return available;
	}

	/**
	 * Create a preprocessor of the specified type.
	 * 
	 * @param preprocessor
	 *            preprocessor needed.
	 * @return the preprocessor, null if it can't be created.
	 * @throws NoSuchDirectoryException
	 *             if the directory where the executable file should be doesn't exist
	 */
	public PreProcessor createPreProcessor(EPreProcessor preprocessor) throws NoSuchDirectoryException {
		try
		{
			switch (preprocessor) {
			case REVIVAL:
				return new ReViVal(directory);
			case SATELLITE_1_0:
				return new SatEllite1_0(directory);
			case HYPRE:
				return new Hypre(directory);
			default:
				return null;
			}
		} catch (IOException e)
		{
			throw new NoSuchDirectoryException();
		}
	}

	/**
	 * Create a {@link SatEllite1_0} preprocessor.
	 * 
	 * @return the preprocessor, null if it can't be created.
	 * @throws NoSuchDirectoryException
	 *             if the directory where the executable file should be doesn't exist
	 */
	public SatEllite1_0 createSatellitePreProcessor() throws NoSuchDirectoryException {
		try
		{
			return new SatEllite1_0(directory);
		} catch (IOException e)
		{
			throw new NoSuchDirectoryException();
		}
	}


	/**
	 * Create a {@link ReViVal} preprocessor.
	 * 
	 * @return the preprocessor, null if it can't be created.
	 * @throws NoSuchDirectoryException
	 *             if the directory where the executable file should be doesn't exist
	 */
	public ReViVal createRevivalPreProcessor() throws NoSuchDirectoryException {
		try
		{
			return new ReViVal(directory);
		} catch (IOException e)
		{
			throw new NoSuchDirectoryException();
		}
	}


	/**
	 * Create a {@link Hypre} preprocessor.
	 * 
	 * @return the preprocessor, null if it can't be created.
	 * @throws NoSuchDirectoryException
	 *             if the directory where the executable file should be doesn't exist
	 */
	public Hypre createHyprePreProcessor() throws NoSuchDirectoryException {
		try
		{
			return new Hypre(directory);
		} catch (IOException e)
		{
			throw new NoSuchDirectoryException();
		}
	}

	/**
	 * 
	 * @return the default directory where the executable file are suppose to be.
	 */
	public File getDirectory() {
		return directory;
	}

	/**
	 * 
	 * Set the default directory where the executable file are suppose to be.
	 */
	public void setDirectory(File directory) {
		this.directory = directory;
	}
}
